package com.yjt.admin.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjt.admin.system.dao.ComuseGroupMapper;
import com.yjt.admin.system.entity.ComuseGroup;
import com.yjt.admin.system.entity.EmpGroup;
import com.yjt.admin.system.form.ComuseGroupForm;
import com.yjt.admin.system.param.ComuseGroupParam;
import com.yjt.admin.system.service.ComuseGroupService;
import com.yjt.admin.system.service.EmpGroupService;
import com.yjt.admin.system.vo.ComuseGroupVO;
import com.yjt.common.utils.ComUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 常用群组表 服务实现类
 * </p>
 *
 * @author zzp
 * @since 2021-10-12
 */
@Service
public class ComuseGroupServiceImpl extends ServiceImpl<ComuseGroupMapper, ComuseGroup> implements ComuseGroupService {

    @Autowired
    private EmpGroupService empGroupService;

    @Resource
    private ComuseGroupMapper comuseGroupMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addComUseGroup(ComuseGroupForm comuseGroupForm) {

        //保存常用群组数据
        ComuseGroup comuseGroupPo = new ComuseGroup();
        BeanUtils.copyProperties(comuseGroupForm, comuseGroupPo);
        this.save(comuseGroupPo);

        //保存群组与人员关联表
        if (ComUtil.isNotNullList(comuseGroupForm.getEmplyIdList())) {
            List<EmpGroup> empGroupPoList = comuseGroupForm.getEmplyIdList().stream().map(emplyId ->
                    new EmpGroup(null, emplyId, comuseGroupPo.getComGroupId())).collect(Collectors.toList());
            empGroupService.saveBatch(empGroupPoList);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateComUseGroup(ComuseGroupForm comuseGroupForm) {

        //保存常用群组数据
        ComuseGroup comuseGroupPo = new ComuseGroup();
        BeanUtils.copyProperties(comuseGroupForm, comuseGroupPo);
        this.updateById(comuseGroupPo);

        //删除群主与人员关联表
        LambdaUpdateWrapper<EmpGroup> lambdaUpdateWrapper = Wrappers.<EmpGroup>lambdaUpdate().eq(EmpGroup::getGroupId, comuseGroupForm.getComGroupId());
        empGroupService.remove(lambdaUpdateWrapper);

        if (ComUtil.isNotNullList(comuseGroupForm.getEmplyIdList())) {
            //保存群主与人员关联表
            List<EmpGroup> empGroupPoList = comuseGroupForm.getEmplyIdList().stream().map(emplyId ->
                    new EmpGroup(null, emplyId, comuseGroupPo.getComGroupId())).collect(Collectors.toList());
            empGroupService.saveBatch(empGroupPoList);
        }


    }

    @Override
    public ComuseGroupVO getOneComUseGroup(String comGroupId) {
        //查询常用组别信息
        ComuseGroupVO comuseGroupVO = comuseGroupMapper.findOneComUseGroup(comGroupId);

        //查询常用群组人员信息
        if (ComUtil.isNotNullStr(comuseGroupVO.getEmplyIds())) {
            comuseGroupVO.setEmplyIdList(Arrays.asList(comuseGroupVO.getEmplyIds().split(",")));
        }
        if (ComUtil.isNotNullStr(comuseGroupVO.getEmplyNames())) {
            comuseGroupVO.setEmplyNameList(Arrays.asList(comuseGroupVO.getEmplyNames().split(",")));
        }
        return comuseGroupVO;
    }

    @Override
    public IPage<ComuseGroupVO> getPageComUseGroup(ComuseGroupParam comuseGroupParam) {
        return comuseGroupMapper.findPageComUseGroup(comuseGroupParam.toPage(),comuseGroupParam);
    }
}
